package com.example.wangliang.piechartview;

import android.util.Log;

/**
 * Created by lenovo on 2016/7/25、
 * 功能作用：依圆心坐标，半径，扇形角度，计算出扇形终射线与圆弧交叉点的xy坐标
 *
 */
public class XChartCalc {
    //Position位置
    private float posX = 0.0f;
    private float posY = 0.0f;
    private Float cirX;
    private Float cirY;
    private Float radius;
    private Float cirAngle;


    /**
     * 设置扇形角度
     * @param cirAngle
     */
    public XChartCalc setCirAngle(float radius,float cirAngle){
        this.cirAngle = cirAngle;
        this.radius = radius;
        if(cirX == null || cirY == null ){
            try {
                throw new Exception();
            } catch (Exception e) {
                Log.e("Exception", "Exception: 其他参数不能为空", e);
            }
        }else {
            CalcArcEndPointXY(cirX, cirY, radius, cirAngle);
        }
        return this;
    }

    public void CalcArcEndPointXY(float cirX, float cirY){
        this.cirX = cirX;
        this.cirY = cirY;
    }


    //依圆心坐标，半径，扇形角度，计算出扇形终射线与圆弧交叉点的xy坐标
    public void CalcArcEndPointXY(float cirX, float cirY, float radius, float cirAngle){
        this.cirX = cirX;
        this.cirY = cirY;
        this.radius = radius;
        this.cirAngle = cirAngle;

        //将角度转换为弧度
        float arcAngle = (float) (Math.PI * cirAngle / 180.0);
        if (cirAngle < 90)
        {
            posX = cirX + (float)(Math.cos(arcAngle)) * radius;
            posY = cirY + (float)(Math.sin(arcAngle)) * radius;
        }
        else if (cirAngle == 90)
        {
            posX = cirX;
            posY = cirY + radius;
        }
        else if (cirAngle > 90 && cirAngle < 180)
        {
            arcAngle = (float) (Math.PI * (180 - cirAngle) / 180.0);
            posX = cirX - (float)(Math.cos(arcAngle)) * radius;
            posY = cirY + (float)(Math.sin(arcAngle)) * radius;
        }
        else if (cirAngle == 180)
        {
            posX = cirX - radius;
            posY = cirY;
        }
        else if (cirAngle > 180 && cirAngle < 270)
        {
            arcAngle = (float) (Math.PI * (cirAngle - 180) / 180.0);
            posX = cirX - (float)(Math.cos(arcAngle)) * radius;
            posY = cirY - (float)(Math.sin(arcAngle)) * radius;
        }
        else if (cirAngle == 270)
        {
            posX = cirX;
            posY = cirY - radius;
        }
        else
        {
            arcAngle = (float) (Math.PI * (360 - cirAngle) / 180.0);
            posX = cirX + (float)(Math.cos(arcAngle)) * radius;
            posY = cirY - (float)(Math.sin(arcAngle)) * radius;
        }
    }


    //////////////
    public float getPosX() {
        return posX;
    }


    public float getPosY() {
        return posY;
    }
    //////////////
}
